<?php
/**
 * @copyright
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */

declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\User as UserList;
use dateset\Dateset;
use think\facade\Db;
use think\facade\View;

class User extends BaseController
{
    public function index()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['nickname|mobile', 'like', '%' . $param['keywords'] . '%'];
            }
            if (!empty($param['user_type'])) {
                $where[] = ['user_type', '=', $param['user_type']];
            }
            if (!empty($param['is_vip'])) {
                if ($param['is_vip'] == 1) {
                    $where[] = ['is_vip_one', '=', 0];
                    $where[] = ['is_vip_two', '=', 0];
                    $where[] = ['is_svip_one', '=', 0];
                    $where[] = ['is_svip_two', '=', 0];
                } else {
                    $where[] = ['is_vip_one|is_vip_two|is_svip_one|is_svip_two', '>', 0];
                }

            }
            //按时间检索
            $start_time = isset($param['start_time']) ? strtotime(urldecode($param['start_time'])) : 0;
            $end_time = isset($param['end_time']) ? strtotime(urldecode($param['end_time'])) : 0;

            if ($start_time > 0 && $end_time > 0) {
                if ($start_time === $end_time) {
                    $where[] = ['register_time', '=', $start_time];
                } else {
                    $where[] = ['register_time', '>=', $start_time];
                    $where[] = ['register_time', '<=', $end_time];
                }
            } elseif ($start_time > 0 && $end_time == 0) {
                $where[] = ['register_time', '>=', $start_time];
            } elseif ($start_time == 0 && $end_time > 0) {
                $where[] = ['register_time', '<=', $end_time];
            }

            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $content = UserList::where($where)
                ->order('id desc')
                ->paginate($rows, false, ['query' => $param])
                ->each(function ($item, $key) {
                    $item->register_time = empty($item->register_time) ? '-' : date('Y-m-d H:i', $item->register_time);
                    // $item->level_name = Db::name('UserLevel')->where(['id' => $item->level])->value('title');
                })->toArray();
            if (!empty($content['data'])) {
                foreach ($content['data'] as &$val) {
                    $member = [];
                    if (!empty($val['is_vip_one'])) {
                        $member[] = '个人VIP(到期:'.date('Y-m-d',$val['vip_end_time_one']) . ')';
                    }
                    if (!empty($val['is_vip_two'])) {
                        $member[] = '企业VIP(到期:'.date('Y-m-d',$val['vip_end_time_two']) . ')';
                    }
                    if (!empty($val['is_svip_one'])) {
                        $member[] = '个人SVIP(到期:'.date('Y-m-d',$val['svip_end_time_one']) . ')';
                    }
                    if (!empty($val['is_svip_two'])) {
                        $member[] = '企业SVIP(到期:'.date('Y-m-d',$val['svip_end_time_two']) . ')';
                    }
                    if (!empty($member)) {
                        $val['member'] = implode(',',$member);
                    } else {
                        $val['member'] = '普通用户';
                    }

                }
            }
            return table_assign(0, '', $content);
        } else {
            return view();
        }
    }
    public function member()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['nickname|mobile', 'like', '%' . $param['keywords'] . '%'];
            }
            $where[] = ['is_vip_one|is_vip_two|is_svip_one|is_svip_two','>',0];
            //按时间检索
            $start_time = isset($param['start_time']) ? strtotime(urldecode($param['start_time'])) : 0;
            $end_time = isset($param['end_time']) ? strtotime(urldecode($param['end_time'])) : 0;

            if ($start_time > 0 && $end_time > 0) {
                if ($start_time === $end_time) {
                    $where[] = ['register_time', '=', $start_time];
                } else {
                    $where[] = ['register_time', '>=', $start_time];
                    $where[] = ['register_time', '<=', $end_time];
                }
            } elseif ($start_time > 0 && $end_time == 0) {
                $where[] = ['register_time', '>=', $start_time];
            } elseif ($start_time == 0 && $end_time > 0) {
                $where[] = ['register_time', '<=', $end_time];
            }

            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $content = UserList::where($where)
                ->order('id desc')
                ->paginate($rows, false, ['query' => $param])
                ->each(function ($item, $key) {
                    $item->register_time = empty($item->register_time) ? '-' : date('Y-m-d H:i', $item->register_time);
                    // $item->level_name = Db::name('UserLevel')->where(['id' => $item->level])->value('title');
                })->toArray();
            if (!empty($content['data'])) {
                foreach ($content['data'] as &$val) {
                    $member = [];
                    if (!empty($val['is_vip_one'])) {
                        $member[] = '个人VIP';
                    }
                    if (!empty($val['is_vip_two'])) {
                        $member[] = '企业VIP';
                    }
                    if (!empty($val['is_svip_one'])) {
                        $member[] = '个人SVIP';
                    }
                    if (!empty($val['is_svip_two'])) {
                        $member[] = '企业SVIP';
                    }
                    if (!empty($member)) {
                        $val['member'] = implode(',',$member);
                    } else {
                        $val['member'] = '';
                    }
                }
            }
            return table_assign(0, '', $content);
        } else {
            return view();
        }
    }
    public function indexs()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            $where[] = ['user_type', '=', 1];
            if (!empty($param['keywords'])) {
                $where[] = ['nickname|mobile', 'like', '%' . $param['keywords'] . '%'];
            }

            //按时间检索
            $start_time = isset($param['start_time']) ? strtotime(urldecode($param['start_time'])) : 0;
            $end_time = isset($param['end_time']) ? strtotime(urldecode($param['end_time'])) : 0;

            if ($start_time > 0 && $end_time > 0) {
                if ($start_time === $end_time) {
                    $where[] = ['register_time', '=', $start_time];
                } else {
                    $where[] = ['register_time', '>=', $start_time];
                    $where[] = ['register_time', '<=', $end_time];
                }
            } elseif ($start_time > 0 && $end_time == 0) {
                $where[] = ['register_time', '>=', $start_time];
            } elseif ($start_time == 0 && $end_time > 0) {
                $where[] = ['register_time', '<=', $end_time];
            }

            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $content = UserList::where($where)
                ->order('id desc')
                ->paginate($rows, false, ['query' => $param])
                ->each(function ($item, $key) {
                    $item->register_time = empty($item->register_time) ? '-' : date('Y-m-d H:i', $item->register_time);
                    // $item->level_name = Db::name('UserLevel')->where(['id' => $item->level])->value('title');
                });
            return table_assign(0, '', $content);
        } else {
            return view();
        }
    }

    public function indexss()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            $where[] = ['user_type', '=', 2];
            if (!empty($param['keywords'])) {
                $where[] = ['company', 'like', '%' . $param['keywords'] . '%'];
            }

            //按时间检索
            $start_time = isset($param['start_time']) ? strtotime(urldecode($param['start_time'])) : 0;
            $end_time = isset($param['end_time']) ? strtotime(urldecode($param['end_time'])) : 0;

            if ($start_time > 0 && $end_time > 0) {
                if ($start_time === $end_time) {
                    $where[] = ['register_time', '=', $start_time];
                } else {
                    $where[] = ['register_time', '>=', $start_time];
                    $where[] = ['register_time', '<=', $end_time];
                }
            } elseif ($start_time > 0 && $end_time == 0) {
                $where[] = ['register_time', '>=', $start_time];
            } elseif ($start_time == 0 && $end_time > 0) {
                $where[] = ['register_time', '<=', $end_time];
            }

            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $content = UserList::where($where)
                ->order('id desc')
                ->paginate($rows, false, ['query' => $param])
                ->each(function ($item, $key) {
                    $item->register_time = empty($item->register_time) ? '-' : date('Y-m-d H:i', $item->register_time);
                    // $item->level_name = Db::name('UserLevel')->where(['id' => $item->level])->value('title');
                });
            return table_assign(0, '', $content);
        } else {
            return view();
        }
    }

    public function bu()
    {
        if (request()->isAjax()) {
            $param = request()->param();
            if (empty($param['score'])) {
                return error('充值数量不能为空或0');
            }
            if ($param['type'] == 1) {
                $title = '充值积分(后台)';
                $type = 1;
                Db::name('user')->where('id',$param['id'])->inc('score',floatval($param['score']))->update();
            } else {
                $title = '扣除积分(后台)';
                $type = 2;
                Db::name('user')->where('id',$param['id'])->dec('score',floatval($param['score']))->update();
            }
            Db::name('score_log')->insert([
                'uid' => $param['id'],
                'title' => $title,
                'type' => $type,
                'num' => $param['score'],
                'create_time' => time()
            ]);
            return success('操作成功');
        }
    }

    //编辑
    public function edit()
    {
        $param = get_params();
        if (request()->isAjax()) {
            if (!empty($param['id']) && $param['id'] > 0) {
                //当前手机号是否已注册
                $where = [
                    ['mobile', '=', $param['mobile']],
                    ['id', '<>', $param['id']],
                ];
                if (Db::name('user')->where($where)->count() > 0) {
                    return to_assign(1, '该手机号已注册');
                }
                $param['update_time'] = time();
                if (!empty($param['vip_end_time_one'])) {
                    $param['vip_end_time_one'] = strtotime($param['vip_end_time_one']);
                } else {
                    $param['vip_end_time_one'] = 0;
                }
                if (!empty($param['vip_end_time_two'])) {
                    $param['vip_end_time_two'] = strtotime($param['vip_end_time_two']);
                } else {
                    $param['vip_end_time_two'] = 0;
                }
                if (!empty($param['svip_end_time_one'])) {
                    $param['svip_end_time_one'] = strtotime($param['svip_end_time_one']);
                } else {
                    $param['svip_end_time_one'] = 0;
                }
                if (!empty($param['svip_end_time_two'])) {
                    $param['svip_end_time_two'] = strtotime($param['svip_end_time_two']);
                } else {
                    $param['svip_end_time_two'] = 0;
                }
                $res = Db::name('User')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
                if ($res !== false) {
                    add_log('edit', $param['id'], $param);
                    return to_assign();
                } else {
                    return to_assign(1, '提交失败');
                }
            }
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            $user = Db::name('User')->where(['id' => $id])->find();
            if (!empty($user['vip_end_time_one'])) {
                $user['vip_end_time_one'] = date('Y-m-d',$user['vip_end_time_one']);
            } else {
                $user['vip_end_time_one'] = '';
            }
            if (!empty($user['vip_end_time_two'])) {
                $user['vip_end_time_two'] = date('Y-m-d',$user['vip_end_time_two']);
            } else {
                $user['vip_end_time_two'] = '';
            }
            if (!empty($user['svip_end_time_one'])) {
                $user['svip_end_time_one'] = date('Y-m-d',$user['svip_end_time_one']);
            } else {
                $user['svip_end_time_one'] = '';
            }
            if (!empty($user['svip_end_time_two'])) {
                $user['svip_end_time_two'] = date('Y-m-d',$user['svip_end_time_two']);
            } else {
                $user['svip_end_time_two'] = '';
            }
            View::assign('user', $user);
            return view();
        }
    }

    //查看
    public function view()
    {
        $id = empty(get_params('id')) ? 0 : get_params('id');
        $user = Db::name('User')->where(['id' => $id])->find();
        $member = [];
        if (!empty($user['is_vip_one'])) {
            $member[] = '个人VIP(到期:'.date('Y-m-d',$user['vip_end_time_one']) . ')';
        }
        if (!empty($user['is_vip_two'])) {
            $member[] = '企业VIP(到期:'.date('Y-m-d',$user['vip_end_time_two']) . ')';
        }
        if (!empty($user['is_svip_one'])) {
            $member[] = '个人SVIP(到期:'.date('Y-m-d',$user['svip_end_time_one']) . ')';
        }
        if (!empty($user['is_svip_two'])) {
            $member[] = '企业SVIP(到期:'.date('Y-m-d',$user['svip_end_time_two']) . ')';
        }
        if (!empty($member)) {
            $user['member'] = implode(',',$member);
        } else {
            $user['member'] = '普通用户';
        }
        add_log('view', get_params('id'));
        View::assign('user', $user);
        return view();
    }
    //禁用/启用
    public function disable()
    {
        $id = get_params("id");
        $data['status'] = get_params("status");
        $data['update_time'] = time();
        $data['id'] = $id;
        if (Db::name('User')->update($data) !== false) {
            if ($data['status'] == 0) {
                add_log('disable', $id, $data);
            } else if ($data['status'] == 1) {
                add_log('recovery', $id, $data);
            }
            return to_assign();
        } else {
            return to_assign(1, "操作失败");
        }
    }

    public function dels()
    {
        $param = get_params();
        Db::name('user')->where('id',$param['id'])->delete();
        return to_assign();
    }

    //日志
    public function log()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['nickname|content|param_id', 'like', '%' . $param['keywords'] . '%'];
            }
            if (!empty($param['action'])) {
                $where[] = ['title', '=', $param['action']];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $content = DB::name('UserLog')
                ->field("id,uid,nickname,title,content,ip,param_id,param,FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') create_time")
                ->order('create_time desc')
                ->where($where)
                ->paginate($rows, false, ['query' => $param]);

            $content->toArray();
            foreach ($content as $k => $v) {
                $data = $v;
                $param_array = json_decode($v['param'], true);
                $param_value = '';
                foreach ($param_array as $key => $value) {
                    if (is_array($value)) {
                        $value = array_to_string($value);
                    }
                    $param_value .= $key . ':' . $value . '&nbsp;&nbsp;|&nbsp;&nbsp;';
                }
                $data['param'] = $param_value;
                $content->offsetSet($k, $data);
            }
            return table_assign(0, '', $content);
        } else {
            $type_action = get_config('log.user_action');
            View::assign('type_action', $type_action);
            return view();
        }
    }

    //记录
    public function record()
    {
        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['nickname|title', 'like', '%' . $param['keywords'] . '%'];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $content = Db::name('UserLog')
                ->field("id,uid,nickname,title,content,ip,param,create_time")
                ->order('create_time desc')
                ->where($where)
                ->paginate($rows, false, ['query' => $param]);

            $content->toArray();
			$date_set = new Dateset();
            foreach ($content as $k => $v) {
                $data = $v;
                $param_array = json_decode($v['param'], true);
                $name = '';
                if (!empty($param_array['name'])) {
                    $name = '：' . $param_array['name'];
                }
                if (!empty($param_array['title'])) {
                    $name = '：' . $param_array['title'];
                }
                $data['content'] = $v['content'] . $name;
				$data['times'] = $date_set->time_trans($v['create_time']);
                $content->offsetSet($k, $data);
            }
            return table_assign(0, '', $content);
        } else {
            return view();
        }
    }

}
